package com.mb.app.controller;

import com.mb.app.domain.TbColorCfg;
import com.mb.app.service.ITbColorCfgService;
import com.mb.common.controller.BaseController;
import com.mb.common.utils.DateUtils;
import com.mb.common.utils.PageUtils;
import com.mb.common.utils.Query;
import com.mb.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 颜色属性配置  前端控制器
 * </p>
 *
 * @author liliping
 * @since 2019-04-01
 */
@Controller
@RequestMapping("/app/colorCfg")
@Slf4j
public class TbColorCfgController extends BaseController {
    
    
    @Autowired
    private ITbColorCfgService colorCfgService;

    @GetMapping("/index")
    String index(){
        return "/app/colorCfg/colorCfg_list";
    }


    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        // 时间截取
        String createTime = (String) params.get("createTime");
        if (StringUtils.isNotEmpty(createTime)) {
            params.put("startTime", DateUtils.parse(createTime.substring(0,10),DateUtils.DATE_PATTERN));
            params.put("endTime",createTime.substring(13,23)+" 23:59:59");
        }
        Query query = new Query(params);
        List<TbColorCfg> list = colorCfgService.queryList(query);
        int total = colorCfgService.count(query);
        PageUtils pageUtils = new PageUtils(list, total);
        return pageUtils;
    }

    @GetMapping("/add")
    String add(){
        return "/app/colorCfg/colorCfg_add";
    }


    @ResponseBody
    @PostMapping("/save")
    public R save(TbColorCfg TbColorCfg, HttpServletRequest request){
        TbColorCfg.setAddUserId(getUserId());
        int result = colorCfgService.save(TbColorCfg);
        return R.ok();
    }


    @ResponseBody
    @PostMapping("/remove")
    public R remove(@RequestParam("colorId") Long colorId, HttpServletRequest request){
        Long delUserId = getUserId();
        int result = colorCfgService.remove(colorId,delUserId);
        return R.ok();
    }

    @GetMapping("/dtl/{colorCfgId}")
    String dtl(@PathVariable("colorCfgId") Long colorCfg, Model model){
        model.addAttribute("colorCfgInfo",colorCfgService.getDtl(colorCfg));
        return "/app/colorCfg/colorCfg_info";
    }


    @GetMapping("/edit/{colorCfgId}")
    String edit(@PathVariable("colorCfgId") Long colorCfgId, Model model){
        model.addAttribute("colorCfgInfo",colorCfgService.getDtl(colorCfgId));
        return "/app/colorCfg/colorCfg_edit";
    }


    @ResponseBody
    @PostMapping("/update")
    public R update(TbColorCfg TbColorCfg, HttpServletRequest request){
        TbColorCfg.setUpdateUserId(getUserId());
        int result = colorCfgService.update(TbColorCfg);
        return R.ok();
    }

    /**
     * 产品分类是否存在
     */
    @PostMapping("/colorCfgNameExit")
    @ResponseBody
    boolean colorCfgsNameExit(@RequestParam Map<String, Object> params) {
        // 存在，不通过，false
        return !colorCfgService.colorCfgsNameExit(params);
    }

	
}
